package com.biz.ssm.service.impl;

import com.biz.ssm.mapper.AdminMapper;
import com.biz.ssm.mapper.AdminRoleMapper;
import com.biz.ssm.mapper.RoleAuthorityMapper;
import com.biz.ssm.mapper.RoleMapper;
import com.biz.ssm.base.BaseServiceImpl;
import com.biz.ssm.domain.Admin;
import com.biz.ssm.domain.AdminRole;
import com.biz.ssm.domain.Role;
import com.biz.ssm.service.AdminService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 *
 * AdminService
 *
 **/
@Service
public class AdminServiceImpl extends BaseServiceImpl<Admin, Long , AdminMapper> implements AdminService{

	@Resource
	private AdminMapper adminMapper;

	@Resource
	private RoleMapper roleMapper;

	@Resource
	private AdminRoleMapper adminRoleMapper;

	@Resource
	private RoleAuthorityMapper roleAuthorityMapper;

	@Override
	public AdminMapper getMapper() {
		return adminMapper;
	}

	@Override
	public boolean usernameExists(String username) {
		Admin admin = adminMapper.findByUsername(username);
		if (admin == null) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public Admin findByUsername(String username) {
		return adminMapper.findByUsername(username);
	}

	@Override
	public List<String> findAuthorities(Long id) {
		List<String> authorities = new ArrayList<String>();
		Admin admin = adminMapper.selectById(id);
		if (admin != null) {
			for (Role role : roleMapper.getRolesByAdmin(id)) {
				authorities.addAll(roleAuthorityMapper.getAuthoritysByRole(role.getId()));
			}
		}
		return authorities;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteAdmin(Admin admin) {
		this.adminMapper.deleteById(admin.getId());
		adminRoleMapper.delByAdmin(admin.getId());
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void createAdmin(Admin admin, Long[] ids) {
		try {
			adminMapper.insert(admin);
			if (null != ids && ids.length > 0) {
				List<Role> roles = roleMapper.findListByIds(ids);
				for (Role role : roles) {
					AdminRole adminRole = new AdminRole();
					adminRole.setAdmins(admin.getId());
					adminRole.setRoles(role.getId());
					adminRoleMapper.insert(adminRole);
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void modifyAdmin(Admin admin, Long[] ids) {
		try {
			adminMapper.update(admin);
			adminRoleMapper.delByAdmin(admin.getId());
			if(null !=ids && ids.length>0){
				List<Role> roles = roleMapper.findListByIds(ids);
				for (Role role : roles) {
					AdminRole adminRole = new AdminRole();
					adminRole.setAdmins(admin.getId());
					adminRole.setRoles(role.getId());
					adminRoleMapper.insert(adminRole);
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void removeAdmins(Long[] ids) {
		for (int i = 0; i < ids.length; i++) {
			Long id = ids[i];
			Admin admin = adminMapper.selectById(id);
			adminRoleMapper.delByAdmin(id);
			adminMapper.deleteById(admin.getId());
		}
	}
}
